package com.example.administrator.pms;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;

import com.example.administrator.pms.Home.HomeFragment;
import com.example.administrator.pms.Like.LikeFragment;
import com.example.administrator.pms.User.UserFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;

public class MainActivity extends FragmentActivity {


    @Bind(R.id.ivHome)
    ImageView ivHome;
    @Bind(R.id.ivLike)
    ImageView ivLike;
    @Bind(R.id.ivUser)
    ImageView ivUser;
    private FragmentManager fManager;
    private HomeFragment fmHome;
    private LikeFragment fmLike;
    private UserFragment fmUser;

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };
    //    float x1 = 0;
//    float x2 = 0;
//    float y1 = 0;
//    float y2 = 0;
    int page = 0;
    public static void verifyStoragePermissions(FragmentActivity activity) {

        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        verifyStoragePermissions(this);

        ButterKnife.bind(this);
        Bmob.initialize(this, "9ba2d0433b4da0e050f5a6c479449609");

        initView();
        clickMenu(0);
        ivHome = (ImageView) findViewById(R.id.ivHome);
        ivLike = (ImageView) findViewById(R.id.ivLike);
        ivUser = (ImageView) findViewById(R.id.ivUser);
    }

    public void clickMenu(int vID) {
        FragmentTransaction trans = fManager.beginTransaction();
        hideFragment(trans);
        setFragment(vID, trans);
        trans.commit();
        page = vID;
    }

    private void initView() {
        fManager = getSupportFragmentManager();

    }

    public void hideFragment(FragmentTransaction trans) {
        ivHome.setImageResource(R.drawable.homepage);
        ivLike.setImageResource(R.drawable.like);
        ivUser.setImageResource(R.drawable.usernew);
        if (null != fmHome) {
            trans.hide(fmHome);
        }
        if (null != fmLike) {
            trans.hide(fmLike);
        }
        if (null != fmUser) {
            trans.hide(fmUser);
        }
    }

    private void setFragment(int vID, FragmentTransaction trans) {
        switch (vID) {
            case 0:
                ivHome.setImageResource(R.drawable.homepage2);
                if (null == fmHome) {
                    fmHome = new HomeFragment();
                    trans.add(R.id.fm_content, fmHome);
                } else {
                    trans.show(fmHome);

                }
                break;
            case 1:
                ivLike.setImageResource(R.drawable.ic_like);
                if (null == fmLike) {
                    fmLike = new LikeFragment();
                    trans.add(R.id.fm_content, fmLike);
                } else {
                    trans.show(fmLike);

                }
                break;
            case 2:
                ivUser.setImageResource(R.drawable.usernew2);
                if (null == fmUser) {
                    fmUser = new UserFragment();
                    trans.add(R.id.fm_content, fmUser);
                } else {
                    trans.show(fmUser);
                }
                break;
            default:
                break;
        }
    }

    @OnClick({R.id.ivHome, R.id.ivLike, R.id.ivUser})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivHome:
                clickMenu(0);
                break;
            case R.id.ivLike:
                clickMenu(1);
                break;
            case R.id.ivUser:
                clickMenu(2);
                break;
        }
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        //继承了Activity的onTouchEvent方法，直接监听点击事件
//        if(event.getAction() == MotionEvent.ACTION_DOWN) {
//            //当手指按下的时候
//            x1 = event.getX();
//            y1 = event.getY();
//        }
//        if(event.getAction() == MotionEvent.ACTION_UP) {
//            //当手指离开的时候
//            x2 = event.getX();
//            y2 = event.getY();
//            if(y1 - y2 > 50) {
//                //Toast.makeText(MainActivity.this, "向上滑", Toast.LENGTH_SHORT).show();
//            } else if(y2 - y1 > 50) {
//                //Toast.makeText(MainActivity.this, "向下滑", Toast.LENGTH_SHORT).show();
//            } else if(x1 - x2 > 50) {
//                //Toast.makeText(MainActivity.this, "向左滑", Toast.LENGTH_SHORT).show();
//                if(page==0||page==1) {
//                    clickMenu(page + 1);
//                }
//
//            } else if(x2 - x1 > 50) {
//                //Toast.makeText(MainActivity.this, "向右滑", Toast.LENGTH_SHORT).show();
//                if(page==1||page==2) {
//                    clickMenu(page - 1);
//
//                }
//
//            }
//        }
//        return super.onTouchEvent(event);
//    }
}

